package org.dyndns.opendemogroup.optimizer;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import org.junit.Test;

/**
 * A class to test the class ArrayUtil
 */
public class ArrayUtilTest
{

	/**
	 * Tests the <i>shuffle</i> method against the Collections.shuffle(List<?>,
	 * Random) implementation from which it was derived.
	 */
	@Test
	public void shuffle_AgainstReference ( )
	{
		// { initialization
		Random randomSource;
		int length = 20;
		double[] sourceArray = new double[length];
		List<Double> expectedList = new ArrayList<Double> ( length );
		for ( int i = 0; i < length; i++ )
		{
			sourceArray[i] = i;
			expectedList.add ( (double) i );
		}
		// }

		// { double-check
		for ( int i = 0; i < length; i++ )
		{
			String message = "Source array is different at index [" + i + "]";
			assertEquals ( message, expectedList.get ( i ), sourceArray[i] );
		}
		// }

		randomSource = new Random ( 42 );
		ArrayUtil.shuffle ( sourceArray, randomSource );

		randomSource = new Random ( 42 );
		Collections.shuffle ( expectedList, randomSource );

		// { validation
		for ( int i = 0; i < length; i++ )
		{
			String message = "Shuffled array is different at index [" + i + "]";
			assertEquals ( message, expectedList.get ( i ), sourceArray[i] );
		}
		// }
	}

}
